package com.atguigu.controller;

import com.atguigu.entity.UserInfo;
import com.atguigu.entity.bo.LoginBo;
import com.atguigu.entity.bo.RegisterBo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Author:hx
 * @DateTime:2022/9/29 21:24
 * @Description:
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @DubboReference
    private UserInfoService userInfoService;

    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpSession session){
        String code = "1234";
        session.setAttribute("code",code);
        return Result.ok();
    }

    /**
     * @Description 注册
     * @param registerBo:
     * @param session:
     * @return: com.atguigu.result.Result
     */
    @RequestMapping("/register")
    public Result register(@RequestBody RegisterBo registerBo, HttpSession session){
        //校验验证码
        if (!registerBo.getCode().equals(session.getAttribute("code"))){
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //校验手机号
        UserInfo userInfo = userInfoService.getByPhone(registerBo.getPhone());
        if (userInfo != null){
            return Result.build(null,ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //密码加密
        registerBo.setPassword(MD5.encrypt(registerBo.getPassword()));
        userInfo = new UserInfo();
        BeanUtils.copyProperties(registerBo,userInfo);
        //设置用户状态
        userInfo.setStatus(1);
        //注册
        userInfoService.insert(userInfo);

        return Result.ok();
    }

    /**
     * @Description 登录
     * @param loginBo:
     * @param session:
     * @return: com.atguigu.result.Result
     */
    @RequestMapping("/login")
    public Result login(@RequestBody LoginBo loginBo,HttpSession session){
        //验证手机号
        UserInfo userInfo = userInfoService.getByPhone(loginBo.getPhone());
        if (userInfo == null){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        //验证密码
        if (!MD5.encrypt(loginBo.getPassword()).equals(userInfo.getPassword())){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        //验证用户状态
        if (userInfo.getStatus() != 1){
            Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //响应数据
        session.setAttribute("USER",userInfo);

        HashMap<String, Object> map = new HashMap<>();
        map.put("nickName",userInfo.getNickName());
        map.put("phone",userInfo.getPhone());
        return Result.ok(map);
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return Result.ok();
    }
}
